package com.suke.czx.modules.tb.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suke.czx.modules.tb.entity.TbProduct;
import com.suke.czx.modules.tb.entity.TbUserAddress;
import com.suke.czx.modules.tb.vo.OrderVO;
import com.suke.czx.modules.tb.vo.ProductVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.suke.czx.modules.tb.mapper.TbOrderInfoMapper;
import com.suke.czx.modules.tb.entity.TbOrderInfo;
import com.suke.czx.modules.tb.service.TbOrderInfoService;

import java.math.BigDecimal;
import java.util.Map;


/**
 * 订单表
 *
 * @author developer
 * @email developer@163.com
 * @date 2025-05-19 07:16:43
 */
@Service
public class TbOrderInfoServiceImpl extends ServiceImpl<TbOrderInfoMapper, TbOrderInfo> implements TbOrderInfoService {

    @Override
    public IPage<OrderVO> myOrder(String buyerId, Map<String, Object> params) {
        if (StringUtils.isBlank(buyerId)) {
            return new Page<>();
        }
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize"). toString());
        if(!params.containsKey("pageNum")){
            params.put("pageNum", 1);
        }
        if(!params.containsKey("pageSize")){
            params.put("pageSize", 5);
        }
        params.put("buyerId", buyerId);

        String listType = (String) params.get("listType");
        IPage<OrderVO> page = null;
        if ("buy".equals(listType)) {
            page = baseMapper.myOrder(new Page<>(pageNum, pageSize), params);
        }else{
            page = baseMapper.mySellOrder(new Page<>(pageNum, pageSize), params);
        }


        if(null != page && !page.getRecords().isEmpty()){
            page.getRecords().forEach(orderVO -> {
                ProductVO product = JSONUtil.toBean(orderVO.getProductInfoJson(), ProductVO.class);
                orderVO.setProduct(product);
            });
        }

        return page;
    }

    @Override
    public OrderVO getDetailById(String orderId) {
        OrderVO orderVO = baseMapper.getDetailById(orderId);
        if (orderVO == null) {
            return null;
        }
        try {
            if (StringUtils.isNotBlank(orderVO.getProductInfoJson())) {
                ProductVO product = JSONUtil.toBean(orderVO.getProductInfoJson(), ProductVO.class);
                orderVO.setProduct(product);
            }

            if (StringUtils.isNotBlank(orderVO.getShippingAddress())) {
                TbUserAddress userAddress = JSONUtil.toBean(orderVO.getShippingAddress(), TbUserAddress.class);
                orderVO.setAddress(userAddress);
            }
        }catch (Exception e){
            log.error("订单详情解析失败", e);
            return null;
        }
        return orderVO;
    }

    @Override
    public BigDecimal getSellerTotalAmount(String userId) {
        return baseMapper.getSellerTotalAmount(userId);
    }
}
